#! /bin/bash

# Auto Linux Kernel Maker; Automatically un-tars, compiles, and installs a kernel
# Copyright 2009 Anthony Byrne
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

R="\033[0;31m"
E="\033[0m"

# Make sure your in the folder where a .tar.bz2 downloaded from kernel.org is
# MAKE SURE YOUR ROOT WHEN RUNNING THIS!

# Show error if not root

( # Start LOG
echo "---------------------------"

if [ $USER != root ]; then
  echo -e $R"ERROR: Must Be Root"$E
  exit 0
fi

# Warning
echo ""
echo -e $R"Make sure that you launched this program from a directory which has a .tar.bz2 downloaded from kernel.org. If you didn't do this right, KILL this program, don't just hit cancel!"

# Ask and fill variables
echo -e $R"What is the version of the kernel you are trying to install? (e.g 2.6.0) MAKE SURE THIS IS RIGHT!"$E
read KERNEL_VER
echo $KERNEL_VER

# Decompress
echo -e $R"Decompressing Kernel..."$E
tar xivf linux-${KERNEL_VER}.tar.bz2

# Go into Kernel source
cd linux-$KERNEL_VER

# Choose a Method
echo ""
echo ""
echo -e $R"Choose a method for configuration"$E 
echo ""
echo -e $R"1) Configure using the GUI (requires qt3-examples)"$E 
echo -e $R"2) Configure using the Terminal"$E 
echo -e $R"3) Use a pre-existing config file"$E
echo -e $R"Enter the number of your choice"$E
read METHOD
echo $METHOD

if [[ "$METHOD" == "1" ]]; then
make xconfig  
fi

if [[ "$METHOD" == "2" ]]; then
xterm -e make config  
fi

if [[ "$METHOD" == "3" ]]; then
echo -e $R"You've set this script to use a preconfigured kernel config file. Put that config file in the linux-$KERNEL_VER directory that was just created. Make sure that file is named .config"$E
read -n1 -r -p "Press any key to continue..." key
xterm -e make oldconfig  
fi


# Compile the kernel (takes a long time)
echo -e -e $R"Compiling the Kernel (takes a long time)"$E
make 

# Install the kernel modules
echo -e $R"Installing Kernel Modules"$E
make modules_install 


# Install the kernel
echo -e $R"Installing Kernel"$E
make install 


# Crestes a new Initial RAM File System (initramfs)
echo -e $R"Running Post-Installation Tasks (Generating INITRAMFS)"$E 
update-initramfs -k ${KERNEL_VER} -c


# Updates Bootloader
echo -e $R"Running Post-Installation Tasks (Updating GRUB)"$E
update-grub



cd ..

echo -e $R"Do you want to clean up?" 
echo ""
echo -e $R"1) No, Leave everything."$E
echo -e $R"2) Yes, just delete the un-needed files for me."$E 
# echo -e $R"3) Yes, but keep the .config file for the future."$E # see if statement
echo -e $R"Enter the number of your choice"$E
read DELETE
echo $DELETE

if [[ "$DELETE" == "2" ]]; then
rm -r -f linux-$KERNEL_VER
echo -e $R"If you want to use the config file from this kernel in the furture, it is located in /boot/config-${KERNEL_VER}"$E
fi

# Broken. May be fixed in next version
#if [[ "$DELETE" == "3" ]]; then
#cp ./linux-${KERNEL_VER}/.config
#mv ./.config ./dotconfig
#rm -r -f linux-$KERNEL_VER
#fi

# ALL DONE
echo -e $R"All Done! Your new kernel has been installed!"$E


) 2>&1 | tee AKM.log
